public without sharing class SalesBudgetTableDetailHandler {
    
 
    public static void  divide(List<SalesBudgetTableDetail__c> list_new){

        Set<Id>  mset = new Set<Id>();
        Map<String,Id> userMap = new Map<String,Id>();

        for(SalesBudgetTableDetail__c  std : list_new){

            if(std.SalesBudgetTable__c != null ) mset.add(std.SalesBudgetTable__c);
            if(std.TZR__c != null )userMap.put(std.TZR__c, null);
        }
        Map<Id,SalesBudgetTable__c> targetSet =new Map<Id,SalesBudgetTable__c>();

        for(SalesBudgetTable__c st : [select Id, RecordTypeId,CompanyName__c,Year__c,Province__c,ProductLine__c,
            BreakdownRatio1__c,BreakdownRatio2__c,BreakdownRatio3__c,BreakdownRatio4__c
         from SalesBudgetTable__c where Id in: mset and RecordType.DeveloperName = 'BudgetTarget' ]){
            
            targetSet.put(st.Id,st);

        }
        System.debug('=>拆分:'+targetSet.values());
       //if(!targetSet.isEmpty())SalesBudgetTableHandler.divide(targetSet.values());

         if(targetSet.isEmpty())return;

        Id drt =  Utils.getRecordTypeIdByDevNameAndObjName('BudgetBreakdown', 'SalesBudgetTable__c');

        List<SalesBudgetTable__c>  divlist = new List<SalesBudgetTable__c>();
      
        for(User u :[select Id ,Name from User where Name in: userMap.keySet() and IsActive = true ]){
            userMap.put(u.Name, u.Id);
        }
        
        for(SalesBudgetTableDetail__c sbd : list_new){

            SalesBudgetTable__c sbm = targetSet.get(sbd.SalesBudgetTable__c  );

            Id ownerId = userMap.get(sbd.TZR__c);

            
            SalesBudgetTable__c sb = new SalesBudgetTable__c();
            sb.Year__c = sbm.Year__c;
            sb.CompanyName__c = sbd.Company__c;
            sb.Province__c = sbd.Province__c;
            sb.ProductLine__c = sbm.ProductLine__c;
            sb.RecordTypeId = drt;
            sb.BudgetTarget__c = sbd.Id;
            sb.GroupObjectives__c = sbd.TotalBudgetNextYear__c;
            sb.OwnerId = ownerId;
            sb.BreakdownRatio1__c = sbm.BreakdownRatio1__c ; 
            sb.BreakdownRatio2__c = sbm.BreakdownRatio2__c ; 
            sb.BreakdownRatio3__c = sbm.BreakdownRatio3__c ; 
            sb.BreakdownRatio4__c = sbm.BreakdownRatio4__c ; 
            if(ownerId == null){
                sbm.addError('填写人:'+sbd.TZR__c+ '不存在!');
                return ;
            }
            divlist.add(sb);
        }

        if(divlist.size() > 0 ) insert divlist;


    } 

    
    /*
    public static void matchMain(List<SalesBudgetTableDetail__c> list_new ){


        Set<Id>  mset = new Set<Id>();
        for(SalesBudgetTableDetail__c  std : list_new){

            if(std.SalesBudgetTable__c != null ) mset.add(std.SalesBudgetTable__c);

        }
        Map<Id,SalesBudgetTable__c> targetSet =new Map<Id,SalesBudgetTable__c>();
        for(SalesBudgetTable__c st : [select Id, RecordTypeId,CompanyName__c,Year__c,Province__c,ProductLine__c,
            BreakdownRatio1__c,BreakdownRatio2__c,BreakdownRatio3__c,BreakdownRatio4__c
         from SalesBudgetTable__c where Id in: mset and RecordType.DeveloperName = 'BudgetTarget' ]){
            
            targetSet.put(st.Id,st);

        }


        System.debug('=>拆分:'+targetSet.values());

    	//公司+年度+省份
    	Map<String , List<SalesBudgetTableDetail__c> > sdMap = new Map<String , List<SalesBudgetTableDetail__c> >();
    	
    	Map<String,String> productMap  =  new Map<String,String>();
        Map<String,String> ownerMap = new Map<String,String>();

    	for (SalesBudgetTableDetail__c sd: list_new) {

            if(targetSet.containsKey(sd.SalesBudgetTable__c))continue;

    		if(sd.IsAdd__c == true) continue;
//Company__c + Year__c + Province__c + ProductLine__c

    		String key = sd.Company__c+sd.Year__c +sd.Province__c+sd.ProductLine__c ;

    		List<SalesBudgetTableDetail__c> tmp = sdMap.containsKey(key) ? sdMap.get(key) : new List<SalesBudgetTableDetail__c>();

    		productMap.put(sd.NCcode__c,sd.ProductName__c);
    		tmp.add(sd);
    		sdMap.put(key, tmp);

            ownerMap.put(sd.TZR__c, null);

    	}
        if(sdMap.isEmpty())return;
       // BudgetNotMatchOwner
        //匹配所有人
        for (User u  : [select Id,Name from User where (Name in: ownerMap.keySet() or Name =:Label.BudgetNotMatchOwner)  and IsActive = true ]) {
             
            ownerMap.put(u.Name,u.Id);

        }
        


    	//匹配产品
    	for (Product__c p :[select Id,NCcode__c from Product__c where NCcode__c in:productMap.keySet()] ) {
    		productMap.put(p.NCCode__c, p.Id);
    	}




    	//匹配主表
    	Map<String,SalesBudgetTable__c>  stMap = new Map<String,SalesBudgetTable__c>();
    	for(SalesBudgetTable__c st: [select Id ,GroupKey__c from SalesBudgetTable__c where  GroupKey__c in: sdMap.keySet()]){
    		stMap.put(st.GroupKey__c , st);
    	}
    	System.debug(sdMap);
    	for (String key :  sdMap.keySet()) {
    		
    		SalesBudgetTableDetail__c sd = sdMap.get(key)[0];

    		if(!stMap.containsKey(key)){

               // String owner = ownerMap.get(sd.TZR__c);

               // if(owner == null ) owner =  ownerMap.get(Label.BudgetNotMatchOwner);

    			stMap.put(key,new SalesBudgetTable__c(
    														Year__c=sd.Year__c,
    													   CompanyName__c=sd.Company__c,
    														Province__c = sd.Province__c,
                                                            ProductLine__c = sd.ProductLine__c
                                                          //  OwnerId = owner
    														 ));
    		}


    		
    	}


    	upsert stMap.values();
    	System.debug( 'SalesBudgetTableDetailHandler==>'+stMap);
    	for ( String key :  sdMap.keySet() ) {
    		
    		SalesBudgetTable__c st = stMap.get(key);
    		for (SalesBudgetTableDetail__c sd  : sdMap.get(key) ) {
                if(sd.IsAdd__c == true )continue;
    			if(sd.Id == null)sd.SalesBudgetTable__c = st.Id;
    			sd.ProductName__c = productMap.get(sd.NCcode__c);
    		}

    	}

    }*/
}